package com.googlecode.horncomb.util.lang;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
 *
 * @author Daniel
 * @version $LastChangedRevision: 95 $
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	public static String FORMAT_DATE_TIME_WEEK = "yyyy-MM-dd EEEE HH:mm:ss.S";
	public static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss.S";

	public static String FORMAT_DATE_WEEK = "yyyy-MM-dd EEEE";
	public static String FORMAT_DATE = "yyyy-MM-dd";

	public static String FORMAT_TIME = "HH:mm:ss.S";
	public static String FORMAT_TIME_WEEK = "EEEE HH:mm:ss.S";

	/**
	 * 使用预设Format格式化Date成字符串
	 *
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return date == null ? "" : format(date, DateUtils.FORMAT_DATE);
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 *
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String strDate) throws ParseException {
		return StringUtils.isBlank(strDate) ? null : parse(strDate,
				DateUtils.FORMAT_DATE);
	}

	/**
	 * 使用参数Format将字符串转为Date
	 *
	 * @param strDate
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String strDate, String pattern)
			throws ParseException {
		return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
				pattern).parse(strDate);
	}

	/**
	 * 在日期上增加数个整月
	 *
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 在日期上加天
	 *
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addDate(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 在日期上加分钟
	 *
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addMinute(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, n);
		return cal.getTime();
	}

	/**
	 * 在日期上加秒钟
	 *
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addSecond(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, n);
		return cal.getTime();
	}

	/**
	 * 比较日期大于或者等于
	 *
	 * @param left
	 * @param right
	 * @return
	 */
	public static boolean isEqualOrAfter(Date left, Date right) {
		return left.equals(right) || left.after(right);
	}

	/**
	 * 比较日期小于或者等于
	 *
	 * @param left
	 * @param right
	 * @return
	 */
	public static boolean isEqualOrBefore(Date left, Date right) {
		return left.equals(right) || left.before(right);
	}

	/**
	 * 在有效区间中，即左闭右开区间中
	 *
	 * @param date
	 * @param begin
	 * @param end
	 * @return
	 */
	public static boolean isOnEnabledInterval(Date date, Date begin, Date end) {
		return DateUtils.isEqualOrAfter(date, begin) && begin.before(date);
	}

	/**
	 * 获得实际最大日期
	 *
	 * @return
	 */
	public static Date getActualMaximumDate() {
		GregorianCalendar cal = new GregorianCalendar();
		int maxYear = 9999;
		int acMaxYear = cal.getActualMaximum(Calendar.YEAR);
		if (acMaxYear < maxYear) {
			maxYear = acMaxYear;
		}
		cal.set(Calendar.YEAR, 9999);
		cal.set(Calendar.MONTH, cal.getActualMaximum(Calendar.MONTH));
		cal.set(Calendar.DAY_OF_MONTH, cal
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, cal
				.getActualMaximum(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
		cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
		cal.set(Calendar.MILLISECOND, cal
				.getActualMaximum(Calendar.MILLISECOND));
		return cal.getTime();
	}

	/**
	 * 获得实际最小日期
	 *
	 * @return
	 */
	public static Date getActualMinimumDate() {
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, cal.getActualMinimum(Calendar.YEAR));
		cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH));
		cal.set(Calendar.DAY_OF_MONTH, cal
				.getActualMinimum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, cal
				.getActualMinimum(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
		cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
		cal.set(Calendar.MILLISECOND, cal
				.getActualMinimum(Calendar.MILLISECOND));
		return cal.getTime();
	}
}
